import traceback
import redis
import json

# pool必须是单例的
pool = redis.ConnectionPool(host="127.0.0.1", password="",
                            port=6379, max_connections=100, decode_responses=True, encoding="utf8",
                            db=0)  # 创造连接池,最多能放多少链接

# pool_db1 = redis.ConnectionPool(host="192.168.31.143", password="qw123666",
#                                 port=6379, max_connections=100, decode_responses=True, encoding="utf8",
#                                 db=1)  # 创造连接池,最多能放多少链接
#
# pool_db2 = redis.ConnectionPool(host="192.168.31.143", password="qw123666",
#                                 port=6379, max_connections=100, decode_responses=True, encoding="utf8",
#                                 db=2)  # 创造连接池,最多能放多少链接

redis_conn = redis.StrictRedis(connection_pool=pool)  # 取出一个链接


# 默认如果返回结果为空，则默认是缓存三分钟
def util_cache(cache_minute=5):
    def decorator(func):
        def wrapper(*args, **kwargs):
            redis_key = func.__name__ + str(args) + str(kwargs)
            redis_value = redis_conn.get(redis_key)
            if redis_value:
                return json.loads(redis_value).get("data")
            else:
                func_return = func(*args, **kwargs)
                if not func_return:
                    cache_time = 3 * 60
                else:
                    cache_time = cache_minute * 60
                cache_data = json.dumps({"data": func_return}, ensure_ascii=False)
                redis_conn.set(redis_key, cache_data, cache_time)
                return func_return

        return wrapper

    return decorator


def util_cache_v1(func):
    def wrapper(*args, **kwargs):
        redis_key = func.__name__ + str(args) + str(kwargs)
        redis_value = redis_conn.get(redis_key)
        if redis_value:
            return json.loads(redis_value).get("data")
        else:
            func_return = func(*args, **kwargs)
            if not func_return:
                cache_time = 60 * 60
            else:
                cache_time = 7 * 24 * 60 * 60
            cache_data = json.dumps({"data": func_return}, ensure_ascii=False)
            redis_conn.set(redis_key, cache_data, cache_time)
            return func_return

    return wrapper


def util_cache_2hour(func):
    def wrapper(*args, **kwargs):
        redis_key = func.__name__ + str(args) + str(kwargs)
        redis_value = redis_conn.get(redis_key)
        if redis_value:
            return json.loads(redis_value).get("data")
        else:
            func_return = func(*args, **kwargs)
            cache_time = 2 * 60 * 60 - 10
            cache_data = json.dumps({"data": func_return}, ensure_ascii=False)
            redis_conn.set(redis_key, cache_data, cache_time)
            return func_return

    return wrapper


def util_cache_5minute(func):
    def wrapper(*args, **kwargs):
        redis_key = func.__name__ + str(args) + str(kwargs)
        redis_value = redis_conn.get(redis_key)
        if redis_value:
            return json.loads(redis_value).get("data")
        else:
            func_return = func(*args, **kwargs)
            cache_time = 5 * 60 * 60 - 10
            cache_data = json.dumps({"data": func_return}, ensure_ascii=False)
            redis_conn.set(redis_key, cache_data, cache_time)
            return func_return

    return wrapper


@util_cache(cache_minute=10)
def test_demo(a):
    return "test_demo"


if __name__ == "__main__":
    try:
        test_demo(1)
    except Exception as e:
        traceback.print_exc()
